package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.service.CameraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "摄像头控制接口")
@RestController
@RequestMapping("/camera")
public class CameraController {
    
    @Autowired
    private CameraService cameraService;
    
    @GetMapping("/video-url/{deviceSerial}")
    @ApiOperation("获取视频流URL和Token")
    public ResponseEntity<?> getVideoUrl(@PathVariable String deviceSerial) {
        try {
            Map<String, String> urlAndToken = cameraService.getVideoUrlAndToken(deviceSerial);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", urlAndToken);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
    
    @PostMapping("/capture/{deviceSerial}")
    @ApiOperation("抓拍图片并保存")
    public ResponseEntity<?> capture(@PathVariable String deviceSerial) {
        try {
            Map<String, Object> result = cameraService.captureAndSave(deviceSerial);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "抓拍成功");
            response.put("data", result);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/images/{deviceSerial}")
    @ApiOperation("获取设备的历史图片")
    public ResponseEntity<?> getHistoryImages(@PathVariable String deviceSerial) {
        try {
            List<Map<String, Object>> images = cameraService.getHistoryImages(deviceSerial);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", images);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/image/{imageId}")
    @ApiOperation("获取指定ID的图片URL")
    public ResponseEntity<?> getImageUrl(@PathVariable Long imageId) {
        try {
            String imageUrl = cameraService.getImageUrl(imageId);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", imageUrl);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 